<template>
  <el-form>
    <el-form-item
      label="手机号"
      required=""
    >
      <el-input
        v-model="forget.phone"
        placeholder="请输入"
        maxlength="11"
      /><el-button type="primary" size="mini" @click="checkPhone">获取验证码</el-button>
    </el-form-item>
    <el-form-item
      label="验证码"
      required=""
    >
      <el-input v-model="forget.check_ma" maxlength="6" placeholder="请输入验证码" />
    </el-form-item>
    <el-form-item
      label="设置新密码"
      required=""
    >
      <el-input v-model="forget.new_password" placeholder="请输入" show-password />
    </el-form-item>
    <el-form-item
      label="确认新密码"
      required=""
    >
      <el-input v-model="forget.ensure_password" placeholder="请输入" show-password />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">修改密码</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { getCheckMa, restPW } from '@/api/user'
export default {
  data() {
    return {
      forget: {
        phone: '',
        password: '',
        new_password: '',
        ensure_password: '',
        check_ma: ''
      }
    }
  },
  methods: {
    checkPhone() {
      if (this.forget.phone.length === 11) {
        getCheckMa(this.forget).then(res => {
          if (res.code === '20000') {
            this.$message.success('验证码已发送!')
          } else if (res.code === '30001') {
            this.$message.warning('手机号不存在!')
          } else {
            this.$message.info('服务器异常, 请稍后再试!')
          }
        })
      } else {
        this.$message.warning('请填写正确的手机号!')
      }
    },
    submit() {
      if (this.forget.phone !== '' && this.forget.check_ma !== '' && this.forget.new_password !== '' && this.forget.ensure_password !== '') {
        if (this.forget.new_password === this.forget.ensure_password) {
          this.forget.password = this.forget.new_password
          restPW(this.forget).then(res => {
            if (res.code === '20000') {
              this.$message.success('密码更改成功!')
              this.forget = {}
              this.$router.push({ path: '/login' })
            } else if (res.code === '30001') {
              this.$message.warning('验证码错误!')
            } else {
              this.forget = {}
              this.$message.info('服务器异常, 请稍后再试!')
            }
          })
        } else {
          this.$message.warning('新密码填写不一致!')
        }
      } else {
        this.$router.push({ path: '/login' })
        this.$message.warning('请将信息填写完整!!')
      }
    }
  }
}
</script>
